package utils;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import components.RPSList;


/**
 * A GUI component to show the list of players.
 *
 * @see {@link utils.PlayerList}
 */
public class PlayerListGUI extends RPSList implements Observer {

	private static final long serialVersionUID = 1L;
	
	private PlayerList playerList;

	public PlayerListGUI (PlayerList playerList) {
		super ();			
		this.playerList = playerList;  
	    init();
	}

	private void init() {
		try
		{
			playerList.addObserver (this);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	    refresh ();
	}
	
	public Player getSelectedUser () {
		return (Player)getSelectedValue();
	}
	
	public String getSelectedUsername () {
		return ((Player)getSelectedValue()).getPlayerName();
	}	

	private void refresh () {
    	Vector<Player> playerObjects = playerList.getPlayers();	   	
		setListData (playerObjects);
		repaint();
	}

    public void update (Observable obs, Object str) {
        refresh ();
    }
}
